import java.io.Console;
import java.text.ParseException;

import lexer.TLexerGramatica;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RuleReturnScope;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.DOTTreeGenerator;
import org.antlr.stringtemplate.StringTemplate;

import antlr.ParseTree;
import antlr.RecognitionException;
import antlr.TokenStream;

import parser.TPraserGramatica;
import preprocesador.PreProcesador;
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			String  firstParameter = args[0];
			
			String  FileParameter = args[1];
			
			boolean isFileParameter=false;
			
			if (firstParameter.toLowerCase().equals("-f"))
				isFileParameter=true;			
			else
				FileParameter = firstParameter;
			
			PreProcesador preprocesador = new PreProcesador(FileParameter,isFileParameter);
			System.out.println("Pre procesing.");
			String preprocesedcontent = preprocesador.preprocess();
			System.out.println("preprocesedcontent: " + preprocesedcontent);
			System.out.println("About to compile.");
			
			
			TLexerGramatica lexer = new TLexerGramatica(new ANTLRStringStream(preprocesedcontent));
			
			TPraserGramatica parser = new TPraserGramatica(new CommonTokenStream(lexer));
		    CommonTree tree = (CommonTree)parser.start().getTree();
		   
		    
		    printTree(tree, 1);	
		    
		}
		catch(Exception e) {
			System.out.println(e.getMessage());
		}
		
		

	}
	
	public static void printTree(CommonTree t, int indent) {
		if (t != null) {
			StringBuffer sb = new StringBuffer(indent);
			for (int i = 0; i < indent; i++)
				sb = sb.append("   ");
			for (int i = 0; i < t.getChildCount(); i++) {
				System.out.println(sb.toString() + t.getChild(i).toString()
						+ "  " + t.getChild(i).getChildCount());
				printTree((CommonTree) t.getChild(i), indent + 1);
			}
		}
	}
	
	


		
			
}
	



